package com.vess.ratingweb;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {

	private static final int DATABASE_VERSION = 2;
	
	private final Context mContext;
	
	private DatabaseHelper mDbHelper;
	
	public static SQLiteDatabase mDB;

	public static final String IMG_PATH = "img_path";

	public static final String WELCOME_TEXT = "welcome_text";

	public static final String WELCOME_IMG = "welcome_img";

	private static final String CREATE_TEMPLATE_TABLE = "CREATE TABLE WELCOME (" +
			"img_path longtext , " +
			"welcome_text longtext, " +
			"welcome_img longtext " +
			");";
	private static final String DATABASE_NAME = "rating_web";
	private static final String INSERT = "INSERT INTO WELCOME VALUES(" +
			"''," +
			"'Welcome to Piatti Ristorate! We value your opinion, please share your experience by clicking on the stars below.'" +
			",''" +
			")";
	private ContentValues content;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		// tao table LISTTASK
		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.i("createdb", "passed");
			db.execSQL(CREATE_TEMPLATE_TABLE);
			db.execSQL(INSERT);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			onCreate(db);
		}
	}

	public DBAdapter(Context ctx) {
		this.mContext = ctx;
	}

	public DBAdapter open() {
		mDbHelper = new DatabaseHelper(mContext, DATABASE_NAME, null,
				DATABASE_VERSION);
		mDB = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public void insertData(String strTable, ContentValues content) {
		mDB.insert(strTable, null, content);
	}
	
	public void updateData(String strTable, ContentValues content) {
		mDB.update(strTable, content, null, null);
	}
	
	public ArrayList<String> selectTable(String strTable) {
		ArrayList<String> content = new ArrayList<String>();
		Cursor cur = mDB.query(strTable, null, null, null, null, null, null);
		cur.moveToFirst();
		String con = "";
		con = cur.getString(cur.getColumnIndexOrThrow(IMG_PATH));
		Log.i("", cur.getString(cur.getColumnIndexOrThrow(IMG_PATH))+"");
		Log.i("", cur.getString(cur.getColumnIndexOrThrow(WELCOME_TEXT))+"");
		Log.i("", cur.getString(cur.getColumnIndexOrThrow(WELCOME_IMG))+"");
		content.add(con);
		con = cur.getString(cur.getColumnIndexOrThrow(WELCOME_TEXT));
		content.add(con);
		con = cur.getString(cur.getColumnIndexOrThrow(WELCOME_IMG));
		content.add(con);
		return content;
	}
/*	public ArrayList<SmartCalendarEvent> selectTable(String strTable){
		ArrayList<SmartCalendarEvent> listContent = new ArrayList<SmartCalendarEvent>();
		Cursor cur = mDB.query(strTable, null, null, null, null, null, null);
		Log.i("selectdb", cur+"");
		cur.moveToFirst();
		SmartCalendarEvent event = new SmartCalendarEvent();
		while (cur.isAfterLast() == false) {
			event = getContent(cur);
			listContent.add(event);
			cur.moveToNext();
		}
		cur.close();
		return listContent;
	}
	
	private SmartCalendarEvent getContent(Cursor cur) {
		int id = cur.getInt(cur.getColumnIndex("_id"));
		String name = cur.getString(cur.getColumnIndexOrThrow(NAME));
		int color = cur.getInt(cur.getColumnIndexOrThrow(COLOR));
		String icon_path = cur.getString(cur.getColumnIndexOrThrow(ICON_PATH));
		
		Date startTime = new Date();
		startTime.setTime(cur.getLong(cur.getColumnIndexOrThrow(EventsContentProvider.START_TIME)));
		Date finishTime = new Date();
		finishTime.setTime(cur.getLong(cur.getColumnIndexOrThrow(EventsContentProvider.FINISH_TIME)));
		
		SmartCalendarEvent event = new SmartCalendarEvent(id, name, icon_path, color, new Date(), startTime, finishTime);
		return event;
	}*/

}
